package jp.co.redfox.archclub.poker.hand;

import java.util.ArrayList;
import java.util.List;

import jp.co.redfox.archclub.poker.Card;
import jp.co.redfox.archclub.poker.Hand;
import jp.co.redfox.archclub.poker.Card.Suits;

/**
 * Flash.
 * 
 * @author sideofall
 * 
 */
public class Flash implements HandRestriction {

    @Override
    public boolean matches(Hand hand) {
        List<Card> cards = new ArrayList<>();
        cards.addAll(hand.getCards());
        Card.Suits suit = null;
        for (Card card : cards)
            if (suit == null)
                suit = card.getSuits();
            else if (suit != card.getSuits())
                return false;

        return true;
    }

    @Override
    public int getRequiredNumber(Hand hand) {
        List<Card> cards = new ArrayList<>();
        cards.addAll(hand.getCards());
        Card.Suits suit = null;
        int numberOfCards = cards.size();
        for (Card card : cards)
            if (suit == null)
                suit = card.getSuits();
            else if (suit == card.getSuits())
                numberOfCards--;
        return numberOfCards;
    }

    @Override
    public Card[] getChangeCards(Hand hand) {
        List<Card> cards = new ArrayList<>();
        cards.addAll(hand.getCards());
        Card.Suits suit = null;
        List<Card> changes = new ArrayList<>();
        for (Card card : cards)
            if (suit == null)
                suit = card.getSuits();
            else if (suit != card.getSuits())
                changes.add(card);
        return changes.toArray(new Card[changes.size()]);
    }
}
